package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.heima.behavior.dto.ReadBehaviorDto;
import com.heima.behavior.entity.ApBehaviorEntry;
import com.heima.behavior.entity.ApReadBehavior;
import com.heima.behavior.mapper.ApReadBehaviorMapper;
import com.heima.behavior.service.IApBehaviorEntryService;
import com.heima.behavior.service.IApReadBehaviorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.UserThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP阅读行为表 服务实现类
 * </p>
 *
 * @author GQ
 * @since 2023-07-17
 */
@Service
public class ApReadBehaviorServiceImpl extends ServiceImpl<ApReadBehaviorMapper, ApReadBehavior> implements IApReadBehaviorService {
    @Autowired
    private IApBehaviorEntryService entryService;

    /**
     * 保存阅读行为
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult savReeadbehavior(ReadBehaviorDto dto) {
        User user = UserThreadLocalUtil.get();
        if (user == null || user.getUserId() == null) {
            return ResponseResult.errorResult(401, "请登录后操作！");
        }
//        获取entry_id
        ApBehaviorEntry entry = entryService.getEntry(user.getUserId(), dto.getEquipmentId());
//        查询阅读记录
        LambdaQueryWrapper<ApReadBehavior> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApReadBehavior::getEntryId, entry.getId());
        queryWrapper.eq(ApReadBehavior::getArticleId, dto.getArticleId());
        ApReadBehavior one = this.getOne(queryWrapper);
        if (one == null) {
//            -第一次阅读
//            构建阅读行为对象
            ApReadBehavior apReadBehavior = new ApReadBehavior();
            apReadBehavior.setEntryId(entry.getId());
            apReadBehavior.setArticleId(dto.getArticleId());
            apReadBehavior.setReadDuration(dto.getReadDuration());
            apReadBehavior.setPercentage(dto.getPercentage());
            apReadBehavior.setLoadDuration(dto.getLoadDuration());
            apReadBehavior.setUpdatedTime(new Date());
            apReadBehavior.setCount(1);
            apReadBehavior.setCreatedTime(new Date());
            boolean save = this.save(apReadBehavior);
            if (save) {
                return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
            } else {
                return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
            }
        } else {
//            - 已经有阅读记录
            LambdaUpdateWrapper<ApReadBehavior> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(ApReadBehavior::getEntryId, entry.getId());
            updateWrapper.eq(ApReadBehavior::getArticleId, dto.getArticleId());
            int count = one.getCount() + 1;
            updateWrapper.set(ApReadBehavior::getCount, count);
            updateWrapper.set(ApReadBehavior::getUpdatedTime, new Date());
            boolean update = this.update(updateWrapper);
            if (update) {
                return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
            } else {
                return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
            }

        }

    }
}
